<?php
class UserPlantForm extends BaseForm
{
  public function configure()
  {
      
     if ($this->getOption("user")->getAttribute("selectedUser", false)){
         $user=Doctrine_Core::getTable("sfGuardUser")->find($this->getOption("user")->getAttribute("selectedUser"));
     }else
         $user=NULL;
     
    $this->setWidget('user', new sfWidgetFormDoctrineChoice(array(
                                        'model'=>'sfGuardUser', 
                                        'add_empty'=>True, 
                                        'multiple'=>False,
                                        'default' => isset($user)?$user->getId():FALSE ))
                            );
    $this->setValidator('user', new sfValidatorDoctrineChoice(array(
                                        'model'=>'sfGuardUser', 
                                        'multiple'=>false,
                                        )));        
    if (isset($user)){
         
         
         $associatedPlants= Doctrine_Core::getTable("AssocUserPlant")->getPlantsByUser($user);

         $this->setWidget('assocPlant', new sfWidgetFormDoctrineChoice(array(
                                        'model'=>'Plant',
                                        'expanded'=>True,
                                        'multiple'=>True,
                                        'query' => Doctrine_Query::create()->from('Plant p')->select('id, name')->where("p.type <> 'Special'"),
                                        'default'=>$associatedPlants,
             )));
         $this->setValidator('assocPlant', new sfValidatorDoctrineChoice(array(
                                        'model'=>'Plant', 
                                        'multiple'=>True,
                                        'query' => Doctrine_Query::create()->from('Plant p')->select('id, name')->where("p.type <> 'Special'")
             ))); 
         $this->widgetSchema->setHelp("assocPlant"," Select the plant associated to ".$user->getUsername());
         $this->widgetSchema->setLabel("assocPlant", $user->getUsername()." associated Plants");


    }
    $this->widgetSchema->setHelp("user"," Select the user");
    
    $this->widgetSchema->setNameFormat('userPlant[%s]');
  }  
}

